import { Body, Controller, Get, Inject, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { UserLoginDto } from './dto/user-login.dto';
import { JwtService } from '@nestjs/jwt';
import { Public } from 'src/decorator/public.decorator';
import { requirePermissions } from 'src/decorator/permission.decorator';

@Controller('user')
export class UserController {
  @Inject(JwtService)
  private readonly jwtService: JwtService;

  constructor(private readonly userService: UserService) {}

  @Get('init')
  async initData() {
    return this.userService.initData();
  }
  @Post('login')
  @Public()
  async login(@Body() loginUser: UserLoginDto) {
    const user = await this.userService.login(loginUser);
    const token = this.jwtService.sign({
      user: {
        username: user.username,
        roles: user.roles,
      },
    });
    return {
      token,
    };
  }
  @Post('create')
  @requirePermissions('user:create')
  createUser(@Body() createUserDto: UserLoginDto) {
    return createUserDto;
  }
}
